package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.report.ReportComments;
import com.sinosoft.report.ReportManager;
import com.sinosoft.report.ReportView;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.SystemConfigUtil;

@SuppressWarnings("unchecked")
@Controller
public class CollectReportController extends BaseFormController {

	private ReportManager reportMan;

	@Autowired
	public void setReportManager(ReportManager reportMan) {
		this.reportMan = reportMan;
	}

	@RequestMapping("report/collectreport.html")
	protected ModelAndView countUserList(ReportView vo, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		String group = ReportComments.countStr(vo.getGroup(), ",");
		String order = ReportComments.filterOrder(vo.getGroup(), vo.getOrder(),
				",");
		vo.setStrgroup(group);
		vo.setStrorder(order);
		List<ReportView> list = reportMan.searchCollect(vo);

		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setCollectStatus(
						list.get(i).getCollectStatus().toLowerCase());
			}
			model.addAttribute("listresult", true);
		} else {
			model.addAttribute("listresult", false);
		}
		// 卫星传感器
		if (!CheckEmpty.isEmpty(vo.getSatellte())) {
			String[] senor = SystemConfigUtil.getSystemValue(vo.getSatellte(),
					getServletContext());
			model.addAttribute("senor", senor);
		}
		model.addAttribute("reportview", vo);
		// 卫星
		String[] satellite = SystemConfigUtil.getSystemValue("satellite", this
				.getServletContext());
		model.addAttribute("satellite", satellite);
		return new ModelAndView("report/collectreport", "collectlist", list);
	}

	/**
	 * 采集单图形统计
	 * 
	 * @param starttime
	 * @param endtime
	 * @param stry
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("report/collectpicreport.html")
	protected ModelAndView countUserLists(String starttime, String endtime,
			String stry, HttpServletRequest request,
			HttpServletResponse response) {
		String group = stry;
		if (CheckEmpty.isEmpty(stry) || stry.equals("null") || stry.equals("")) {
			group = "taskStatus";
		}

		List<ReportView> list = reportMan.searchCollect(group, starttime,
				endtime);
		Map<String, Integer> map = new HashMap<String, Integer>();
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			ReportView vo = list.get(i);
			count+=vo.getNum();
			map.put(vo.getValue(), vo.getNum());
		}

		JSONObject jsonobj = new JSONObject();
		jsonobj.put("ingredients", map);
		jsonobj.put("count", count);

		try {
			response.getWriter().write(jsonobj.toString());
		} catch (Exception e) {

		}
		return null;
	}

}
